package com.es.service.impl;

import com.es.entity.SysUser;
import com.es.mapper.SysUserMapper;
import com.es.mybatis.util.MybatisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 描述:
 * MyBatis-
 *
 * @author Derek
 * 创建时间：2022-05-10 9:58
 */
@Slf4j
public class SysUserServiceImpl implements com.es.service.SysUserService {

    @Override
    public boolean save(SysUser sysUser) {
        SqlSession session = MybatisUtils.getSqlSession(false);
        SysUserMapper mapper = session.getMapper(SysUserMapper.class);
        int i = mapper.insert(sysUser);
        session.commit();
        session.close();
        return i>0;
    }

    @Override
    public boolean grantRoles(SysUser sysUser) {
        SqlSession session = MybatisUtils.getSqlSession(false);
        SysUserMapper mapper = session.getMapper(SysUserMapper.class);
        int i = mapper.deleteUserRole(sysUser.getId());
        int j = mapper.insertUserRoles(sysUser);
        session.commit();
        session.close();
        return false;
    }

    @Override
    public SysUser get(Integer id) {
        SqlSession session = MybatisUtils.getSqlSession(false);
        SysUserMapper mapper = session.getMapper(SysUserMapper.class);
        SysUser user = mapper.get(id);
        return user;
    }

    @Override
    public List<SysUser> list() {
        SqlSession session = MybatisUtils.getSqlSession(false);
        SysUserMapper mapper = session.getMapper(SysUserMapper.class);
        List<SysUser> users = mapper.list();
        return users;
    }
}
